/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dca.sistemascontrole.view.components;

import java.awt.Color;
import javax.swing.JButton;

/**
 *
 * @author Rafael
 */
public class CustomButton extends JButton {

    private Color backgroundOnExit;
    private Color backgroundOnRelease;

    public CustomButton() {
        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(226, 226, 226)));
        setContentAreaFilled(false);
        setFocusPainted(false);
        setOpaque(true);

        addMouseListeners();
    }

    private void addMouseListeners() {
        this.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mousePressed(java.awt.event.MouseEvent evt) {
                mousePressedFlatButton(evt);
            }

            @Override
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                mouseReleasedFlatButton(evt);
            }

            @Override
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                mouseEnteredFlatButton(evt);
            }

            @Override
            public void mouseExited(java.awt.event.MouseEvent evt) {
                mouseExitedFlatButton(evt);
            }
        });
    }

    private void mousePressedFlatButton(java.awt.event.MouseEvent evt) {
        if (isEnabled()) {
            backgroundOnRelease = getBackground();

            int red = backgroundOnRelease.getRed() - 10;
            if (red > 255) {
                red = 255;
            } else if (red < 0) {
                red = 0;
            }

            int green = backgroundOnRelease.getGreen() - 10;
            if (green > 255) {
                green = 255;
            } else if (green < 0) {
                green = 0;
            }

            int blue = backgroundOnRelease.getBlue() - 10;
            if (blue > 255) {
                blue = 255;
            } else if (blue < 0) {
                blue = 0;
            }

            setBackground(new Color(red, green, blue));
        }
    }

    private void mouseReleasedFlatButton(java.awt.event.MouseEvent evt) {
        if (isEnabled()) {
            setBackground(backgroundOnRelease);
        }
    }

    private void mouseEnteredFlatButton(java.awt.event.MouseEvent evt) {
        if (isEnabled()) {
            backgroundOnExit = getBackground();

            int red = backgroundOnExit.getRed() + 10;
            if (red > 255) {
                red = 255;
            } else if (red < 0) {
                red = 0;
            }

            int green = backgroundOnExit.getGreen() + 10;
            if (green > 255) {
                green = 255;
            } else if (green < 0) {
                green = 0;
            }

            int blue = backgroundOnExit.getBlue() + 10;
            if (blue > 255) {
                blue = 255;
            } else if (blue < 0) {
                blue = 0;
            }

            setBackground(new Color(red, green, blue));
        }
    }

    private void mouseExitedFlatButton(java.awt.event.MouseEvent evt) {
        if (isEnabled()) {
            setBackground(backgroundOnExit);
        }
    }
}
